import {routes,managerRoutes,superRoutes} from './routes'
import {useStore} from '@/store/useStore'
import router from './index'

export default ()=>{
      const store = useStore();
      let type = store.type;
       // 判定用户类型
       if(type == 1){
         superRoutes.forEach( route=>{
           //动态添加路由
           router.addRoute(route);
          })
          store.setRoutes(superRoutes);
        }else if(type == 2){
          managerRoutes.forEach(route=>{
            //动态添加路由
            router.addRoute(route);
          })
          store.setRoutes(managerRoutes);
        }else{
          routes.forEach(route=>{
            //动态添加路由
            router.addRoute(route);
          })
          store.setRoutes(routes);
        }
    

let whiteList = ['/login','/demo'];

// 路由守卫
router.beforeEach((to,from,next)=>{
  const store = useStore();
  //已经登陆
   if(store.isLogin){
    if(to.path==='/login'){
      next('/home')
    }else{
      next()
    }
  }else{
    if(whiteList.includes(to.path)){
      next()
    }else{
      next('/login')
    }
  }
})
}